/**
 * 
 */
package com.zznode.client.wlanpolling.other;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.service.Wifi;
import com.zznode.client.wlanpolling.utils.NetworkUtils;
import com.zznode.client.wlanpolling.widget.SignalView;

/**
 * @author Pink
 * 
 */
public class NeighbouringAPListActivity0 extends ListActivity {

	private WifiReceiver receiverWifi;
	private List<ScanResult> wifiList;
	private WifiManager mainWifi;
	private NeighbouringAPListAdaptor apListAdaptor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.neighbouring_aplist);
	}

	@Override
	protected void onStart() {
		super.onStart();
		mainWifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

		receiverWifi = new WifiReceiver();
		registerReceiver(receiverWifi, new IntentFilter(
				WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
		
		LinearLayout layout = (LinearLayout)findViewById(R.id.apScanProgressbarLayout);
		layout.setVisibility(View.VISIBLE);
		
		mainWifi.startScan();
	}

	private void initApList() {
		if(wifiList == null || wifiList.isEmpty())
			return;
		
		List<Wifi> list = new ArrayList<Wifi>();
		
		for(ScanResult sr : wifiList) {
			Wifi wifi = new Wifi(sr, null);
			list.add(wifi);
		}
		
		apListAdaptor = new NeighbouringAPListAdaptor(this, list);
		
		setListAdapter(apListAdaptor);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(receiverWifi);
	}

	class WifiReceiver extends BroadcastReceiver {
		public void onReceive(Context c, Intent intent) {
			wifiList = mainWifi.getScanResults();
			
			if(wifiList != null && wifiList.size() > 0) {
				initApList();
			} else {
				LinearLayout noApListLayout = (LinearLayout)findViewById(R.id.noApList);
				noApListLayout.setVisibility(View.VISIBLE);
			}
			
			LinearLayout layout = (LinearLayout)findViewById(R.id.apScanProgressbarLayout);
			layout.setVisibility(View.GONE);
		}
	}
}

class NeighbouringAPListAdaptor extends BaseAdapter {
	private List<Wifi> aplist;
	private Context context;
	
	public NeighbouringAPListAdaptor(Context context, List<Wifi> aplist) {
		this.aplist = aplist;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return aplist.size();
	}

	@Override
	public Object getItem(int position) {
		return aplist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
        if(view == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.neighbouring_childitem, null);
        }
        
        Wifi wifi = aplist.get(position);
        
        TextView txtAPName = (TextView)view.findViewById(R.id.txt_apname);
        txtAPName.setText(wifi.getSSID());
        
        TextView txtBssid = (TextView)view.findViewById(R.id.txt_bssid);
        txtBssid.setText(wifi.getBSSID());
        
        TextView txtChannel = (TextView)view.findViewById(R.id.txt_channel);
        txtChannel.setText("信道 " + NetworkUtils.getChannel(wifi.getFrequency()));
        
        TextView txtFrequence = (TextView)view.findViewById(R.id.txt_frequence);
        txtFrequence.setText("频率 " + wifi.getFrequency());
        
        TextView txtSignal = (TextView)view.findViewById(R.id.txt_signal);
        txtSignal.setText("强度 " + wifi.getLevel());
        
        SignalView signalView = (SignalView)view.findViewById(R.id.img_sigal);
        signalView.setSignal(wifi.getLevel());
        
        TextView txtAuthentication = (TextView)view.findViewById(R.id.txt_authentication);
        txtAuthentication.setText(wifi.getCapabilities());
        
		return view;
	}
}
